/**
 * 
 */
package com.pxingg.equipment.powerswitch.Sevytec;

import java.util.Scanner;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.pxingg.exception.XException;
import com.pxingg.fep.serial.Serial;

/**
 * @author pxingg
 * 
 */
public class PowerRegulation4Test
{
    /**
     * The log of this class
     */
    static Log log = LogFactory.getLog(PowerRegulation4Test.class);

    PowerRegulation4 powerRegulation;

    /**
     * The serial the switch connected
     */
    Serial serial = new Serial("RS485Bus");

    /**
     * The port connect to test
     */
    static final String testPort = "COM1";

    /**
     * The speed in the test
     */
    static final int testSpeed = 9600;

    /**
     * The equipment of the switch
     */
    static final int switchNo = 0x10;

    /**
     * @throws java.lang.Exception
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
    }

    /**
     * @throws java.lang.Exception
     */
    @AfterClass
    public static void tearDownAfterClass() throws Exception
    {
    }

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception
    {
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception
    {
    }

    @Test
    public final void testForMaunlControl()
    {
        final Scanner scanner = new Scanner(System.in);

        if (!serial.isConnected())
        {
            Assert.assertTrue(serial.connect(PowerRegulation4Test.testPort, PowerRegulation4Test.testSpeed));
        }

        powerRegulation = new PowerRegulation4(PowerRegulation4Test.switchNo, serial);

        try
        {
            powerRegulation.connect();
        }
        catch (final XException e)
        {
            System.out.println("Error: " + e.getMessage());
        }
        while (true)
        {
            System.out.print("input the command[light_number, power] :");
            final String input = scanner.nextLine();
            final String info[] = StringUtils.split(input);
            final int lightNo = Integer.parseInt(info[0]);
            final int power = Integer.parseInt(info[1]);

            try
            {
                powerRegulation.powerOn(lightNo, power);
                powerRegulation.queryStatus();
                final int status[] = powerRegulation.getSwitchStatus();
                String message = "Success to to get status ";
                for (int j = 0; j < 4; j++)
                {
                    message += "switch(" + (j + 1) + ") = " + status[j] + ", ";
                }
                PowerRegulation4Test.log.info(message + '\n');

                System.out.println(message);
            }
            catch (final XException e)
            {
                System.out.println("Error: " + e.getMessage());
            }

        }
    }

    /**
     * Test method for {@link com.pxingg.equipment.powerswitch.Sevytec.PowerRegulation4#powerOn(int, int)} .
     */
    @Test
    public final void testPowerOnIntInt()
    {
        if (!serial.isConnected())
        {
            Assert.assertTrue(serial.connect(PowerRegulation4Test.testPort, PowerRegulation4Test.testSpeed));
        }

        for (int i = 1; i < 20; i++)
        {
            powerRegulation = new PowerRegulation4(i, serial);
            try
            {
                powerRegulation.queryStatus();
                System.out.println("equiment no = " + i + " is on the bus");
                // break;
            }
            catch (final XException e)
            {
                System.out.println("i = " + i + ", " + e.getMessage());
            }
        }

        powerRegulation = new PowerRegulation4(PowerRegulation4Test.switchNo, serial);
        try
        {
            PowerRegulation4Test.log.info("try to set the switch 1 at level 30");
            powerRegulation.powerOn(1, 30);
        }
        catch (final XException e)
        {
            System.out.println("when power on " + e.getMessage());
        }
        for (int i = 0; i < 100; i++)
        {

            try
            {
                powerRegulation.queryStatus();

                final int status[] = powerRegulation.getSwitchStatus();
                String info = "i = " + i + ", success to to get status ";
                for (int j = 0; j < 4; j++)
                {
                    info += "switch(" + j + ") = " + status[j] + ", ";
                }
                PowerRegulation4Test.log.info(info + '\n');

            }
            catch (final XException e)
            {
                final String info = "i = " + i + ", " + e.getMessage() + "\n";
                PowerRegulation4Test.log.info(info);

            }
        }
        // try
        // {

        // for (int i = 1; i <= 4; i++)
        // {
        // for (int j = 0; j < 0x50; j++)
        // {
        // powerRegulation.powerOn(i, j);
        // System.out.println("for switch " + i + ", set power to " + j);
        // TimeUtils.mySleep(1000);
        // }
        // }
        // }
        // catch (XException e)
        // {
        // System.out.println(e.getMessage());
        // }

        // System.out.println(powerRegulation);
        serial.disconnect();

    }

    @Test
    public final void testTwoSwitch()
    {
        if (!serial.isConnected())
        {
            Assert.assertTrue(serial.connect(PowerRegulation4Test.testPort, PowerRegulation4Test.testSpeed));
        }
        final PowerRegulation4 powerRegulation1 = new PowerRegulation4(16, serial);
        final PowerRegulation4 powerRegulation2 = new PowerRegulation4(17, serial);
        try
        {
            PowerRegulation4Test.log.info("try to set the switch 1 at level 30 on board1");
            powerRegulation1.powerOn(1, 30);
            PowerRegulation4Test.log.info("try to set the switch 2 at level 30 on board2");
            powerRegulation2.powerOn(2, 30);
        }
        catch (final XException e)
        {
            System.out.println("when power on " + e.getMessage());
        }
        for (int i = 0; i < 100; i++)
        {

            try
            {
                powerRegulation1.queryStatus();

                int status[] = powerRegulation1.getSwitchStatus();
                String info = "switch 1: i = " + i + ", success to to get status ";
                for (int j = 0; j < 4; j++)
                {
                    info += "switch(" + j + ") = " + status[j] + ", ";
                }
                PowerRegulation4Test.log.info(info + '\n');

                powerRegulation2.queryStatus();

                status = powerRegulation2.getSwitchStatus();
                info = "switch 2: i = " + i + ", success to to get status ";
                for (int j = 0; j < 4; j++)
                {
                    info += "switch(" + j + ") = " + status[j] + ", ";
                }
                PowerRegulation4Test.log.info(info + '\n');

            }
            catch (final XException e)
            {
                final String info = "i = " + i + ", " + e.getMessage() + "\n";
                PowerRegulation4Test.log.info(info);

            }
        }
        serial.disconnect();
    }
}
